from ttypes import *
from ServerService import *
from ServerServiceWrapper import ServiceWrapper

class ServiceImp(Iface):
	
  def Open(self, host, port, userName, password, database, charset):
    """
    Parameters:
     - host
     - port
     - userName
     - password
     - database
     - charset
    """
    print database
    self.curDatabase=ServiceWrapper(database,  'dbm')
    return ResultInfo(0,'info','key')
    
  def Execute(self, sql):
    """
    Parameters:
     - sql
    """
    #print sql;
    result= self.curDatabase.execute(sql)
    #print result
    return DataInfoAdapter(result).Adapter()
	
  def Close(self, ):
    pass
	
class DataInfoAdapter:
	def __init__(self, _coreResult):
		self.coreResult =_coreResult
	def Adapter(self):
	    dataTable=DataTable()
	    dataTable.Name=''
	    dataTable.AffectedNum=self.coreResult[3]
	    dataTable.UsedTime=self.coreResult[4]
	    dataTable.Columns=[]
	    dataTable.Rows=[]

	    for fieldName in self.coreResult[1]:
			dataTable.Columns.append(DataColumn(fieldName,1))
	    for row in self.coreResult[2]:
			index=0
			dataRow = DataRow()
			dataRow.ColumnDatas = {}
			print row
			for columnValue in row:
			    print dataTable.Columns
			    dataColumn=dataTable.Columns[index]
			    #print type(dataColumn)
			    #print columnValue
			    if type(columnValue) is type(''):
					dataRow.ColumnDatas[dataColumn]=columnValue
					#pass
			    else:
					dataRow.ColumnDatas[dataColumn]=(columnValue)
					#pass
			    dataTable.Rows.append(dataRow)
			    index+=1
	
		
	    dataSet=DataSet()
	    dataSet.Tables=[]
	    dataSet.Tables.append(dataTable)		
		
	    dataInfo=DataInfo()
	    dataInfo.Status=self.coreResult[0]
	    dataInfo.Key=''
	    dataInfo.Info=self.coreResult[5]
	    dataInfo.Datas=dataSet
	    print dataInfo
	    return dataInfo
	
		
  


